<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
	<base href="<%=basePath%>">
	<title>更新资源</title>
	<script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
	<script type='text/javascript' src='<%=basePath%>dwrsystem/interface/resourceService.js'></script>
	<script type='text/javascript' src='<%=basePath%>dwrsystem/engine.js'></script>
	<script type="text/javascript" src='<%=basePath%>js/validateJS.js'></script>
	<script type="text/javascript" src="<%=basePath%>js/tableForm.js"></script>
</head>

<body onload="bodyInit()">
	<div id="dataObj">
	<div id="displayDiv" class="formlayout" style="width:100%;height:100%;overflow: auto;">
		<c:if test="${!empty resourceEdit}" >
		<form action="<%=basePath%>system-resource/ResourceAction!resourceEdit.action" method="post" name="editForm">
			<input type="hidden" name="tresourcesystem.resourceId" id="tresourcesystem.resourceId" value="${resourceEdit.resourceId }"/>
			<table>
				<tr>	
					<th>资源名称</th>
					<td> 
						${resourceEdit.resourceName }
						<input type="hidden" name="editFormHidden"
						 value="[{type:'input',id:'tresourcesystem.resourceName',name:'tresourcesystem.resourceName',value:'${resourceEdit.resourceName }',showText:'${resourceEdit.resourceName }',maxlength:'25',mustEnter:'yes'}]"/>
					</td>
				</tr>
				<tr>	
					<th>资源地址</th>
					<td>
						${resourceEdit.resourceUrl }
						<input type="hidden" name="editFormHidden"
						 value="[{type:'input',id:'tresourcesystem.resourceUrl',name:'tresourcesystem.resourceUrl',value:'${resourceEdit.resourceUrl }',showText:'${resourceEdit.resourceUrl }',maxlength:'25',mustEnter:'yes'}]"/>
					</td>
				</tr>
				<tr>	
					<th>资源类型</th>
					<td>
					<c:if test="${resourceEdit.resourceType == 1}">普通类型</c:if><c:if test="${resourceEdit.resourceType == 2}">其它类型</c:if>
					<input type="hidden" name="editFormHidden"
						value="[{type:'select',id:'tresourcesystem.resourceType',name:'tresourcesystem.resourceType',value:'1&普通类型|2&其他类型',defaultSelect:'${resourceEdit.resourceType }',
							showText:'<c:if test="${resourceEdit.resourceType == 1}">普通类型</c:if><c:if test="${resourceEdit.resourceType == 2}">其它类型</c:if>'}]"/>					
					</td>
				</tr>
				<tr>	
					<th>资源状态</th>
					<td>
						<c:if test="${resourceEdit.status == 1}">有效</c:if><c:if test="${resourceEdit.status == 0}">无效</c:if>
						<input type="hidden" name="editFormHidden"
							value="[{type:'select',id:'tresourcesystem.status',name:'tresourcesystem.status',value:'1&有效|0&无效',defaultSelect:'${resourceEdit.status }',
								showText:'<c:if test="${resourceEdit.status == 1}">有效</c:if><c:if test="${resourceEdit.status == 0}">无效</c:if>'}]"/>							
					</td>
				</tr>
				<tr>   			
					<th>备注</th>
					<td>
						${resourceEdit.note }
						<input type="hidden" name="editFormHidden"
						 value="[{type:'textarea',id:'tresourcesystem.note',name:'tresourcesystem.note',value:'${resourceEdit.note }',showText:'${resourceEdit.note }'}]"/>
					</td>
				</tr>
			</table>
		</form>
		</c:if>
	</div>
	<div id="delDiv" style="display:none"><!-- 删除信息 -->
		<form action="<%=basePath%>system-resource/ResourceAction!resourceDelFromTree.action" method="post" name="delForm">
			<input type="hidden" name="resourceId" id="resourceId" value="${resourceEdit.resourceId }"/>
		</form>
	</div></div>
</body>
	
<script type="text/javascript">
var mytree;
var rids; //更新资源的id集合，用于更新资源无效时的颜色
var dhxLayout = createLayoutNormal('1C',false,true);

function bodyInit(){
	configLayout();
	mytree = parent.parent.window.MyTree;
}

function configLayout(){
	var a = dhxLayout.cells('a');
	a.hideHeader();
	a.setText('主页面');
	a.fixSize(true, true);
	if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
		createToolbarFromDHXLayoutCell(a,function(toolbar){
		    toolbar.addText("info", 1, "操作:");
		    toolbar.addSeparator("sep1", 2);
		    if(power.getPowerInsert())toolbar.addButton("add", 3, "新增下级资源", "new.gif", "new_dis.gif");
		    if(power.getPowerUpdate()){
		    	toolbar.addButton("update", 4, "修改", "settings.gif", "settings.gif");
		    	toolbar.addButton("submit", 5, "提交", "save.gif", "save_dis.gif");
		    	toolbar.addButton("cancel", 6, "取消", "undo.gif", "undo_dis.gif");
		    	toolbar.hideItem('submit');
		    	toolbar.hideItem('cancel');
		    }
		    if(power.getPowerDelete())toolbar.addButton("delete", 7, "删除", "cut.gif", "cut_dis.gif");
		    toolbar.attachEvent("onClick", function(id) {
		    	if(id == 'add'){
		    		addResource();
		    	}
		    	if(id == 'delete'){
		    		validateDel();
		    	}
		    	if(id == 'update'){
		    		tableForm.tableToForm();
		    		toolbar.showItem('submit');
		    		toolbar.showItem('cancel');
		    		toolbar.hideItem('update');
		    	}
		    	if(id == 'cancel'){
		    		tableForm.formReturnTable();
		    		toolbar.showItem('update');
		    		toolbar.hideItem('cancel');
		    		toolbar.hideItem('submit');
		    	}
		    	if(id == 'submit'){
		    		validateEdit();
		    	}
		    });
		});
	a.attachObject('dataObj');
}

function validateEdit(){

	var rurl = document.getElementById('tresourcesystem.resourceUrl').value;
	if(trim(rurl)==''){parent.parent.parent.notifyWarn('请输入资源地址');return;}
	if (!validatePattern.validateInputZZ(rurl,true)){
		parent.parent.parent.notifyWarn('资源地址非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		return ;
	}
	
	var rname = document.getElementById('tresourcesystem.resourceName').value;
	if(trim(rname)==''){parent.parent.parent.notifyWarn('请输入资源名称');return;}
	if (!validatePattern.validateInputZZ(rname,false)){
		parent.parent.parent.notifyWarn('资源名称非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		return ;
	}
	layoutProgressOn(dhxLayout);
	resourceService.hasTResourceName({resourceId:document.getElementById('tresourcesystem.resourceId').value, resourceName:rname}, function(data){
		if(data == 1){
			parent.parent.parent.notifyWarn('存在相同资源名称');layoutProgressOff(dhxLayout);
			return;
		}else{
			if(document.getElementById('tresourcesystem.status').value != '${resourceEdit.status}'){
				resourceService.updateTResourceForStatusByPK({resourceId:document.getElementById('tresourcesystem.resourceId').value, status:document.getElementById('tresourcesystem.status').value}, function(data){
					rids = data;
					document.forms["editForm"].submit();
					changeNodeColor();
					mytree.editNode(document.getElementById('tresourcesystem.resourceId').value, rname);
				});
			}else{
				document.forms["editForm"].submit();
				mytree.editNode(document.getElementById('tresourcesystem.resourceId').value, rname);
			}
		}
	});
}

function changeNodeColor(){
	for(var i = 0; i < rids.length; i++){
		if(document.getElementById('tresourcesystem.status').value == 0){
			parent.parent.left.window.MyTree.changeColor(rids[i], 'red');
		}else{
			parent.parent.left.window.MyTree.changeColor(rids[i], 'black');
		}
	}
}

function addResource(){
	layoutProgressOn(dhxLayout);
	document.location.href = '<%=basePath%>page/system/resource/resource_add.jsp?resourceId=' + document.getElementById('tresourcesystem.resourceId').value + '&nowurl=edit';
}
	
function validateDel(){
	confirmInfo('删除本资源，会同时删除其下级资源',
		function(choose){
			if('ok' == choose){
				layoutProgressOn(dhxLayout);
				document.forms["delForm"].submit();
				if(mytree.getNodeLevel(document.getElementById('tresourcesystem.resourceId').value) == 1){
					mytree.delNode(document.getElementById('tresourcesystem.resourceId').value);
					parent.parent.window.changeRightPage('<%=basePath%>page/system/resource/resource_listtop.jsp?superResourceId=0');
				}else{
					mytree.delNode(document.getElementById('tresourcesystem.resourceId').value);
				}
			}else{
				return;
			}
		}
	);
}
	
</script>
	
</html>
